'''
Created on Mar 6, 2010

@author: Nick Heiner
'''
from google.appengine.ext import webapp
from google.appengine.api import users
from Utils import render

# Controller
class BaseHandler(webapp.RequestHandler):
    """ The simplest handler, which itself should never be used to generate a page
        as it has no implementation of body() """
    
    HOME = 'Home'
    CART = 'Cart'
    DETAIL = 'Detail'
    
    MSG_ERROR = "error"
    MSG_WARNING = "warning"
    MSG_NOTICE = "notice"
    
    ANON_CART_TIMEOUT = 1800 # 30 minutes
    
    """ A list of messages to print """
    
    def __init__(self):
        self.user = users.get_current_user()
        self._messages = []
        
    def add_message(self, text, message_level):
        """ Adds a message to the queue, to be displayed with severity `message_level`. Blank messages will be ignored. """
        if not (message_level == self.MSG_ERROR or message_level == self.MSG_WARNING or message_level == self.MSG_NOTICE):
            raise ValueError("Invalid message_level supplied to `add_message()`: %s" % message_level)
        if not text is "":
            self._messages.append({'text': text, 'msg_level': message_level})
        
    def render_messages(self):
        for msg in self._messages:
            render('Views/message.html', self, {'text': msg['text'], 'level': msg['msg_level']})
        

            
    def initAnonKey(self):
        """ Set the value of self.anon_key """
        # for some reason, this code doesn't work in __init__. I'm not sure why.
        if not self.user:
            self.anon_key = self.request.cookies.get('anon_cart_key')
            if not self.anon_key:
                self.anon_key = self.request.remote_addr
                self.response.headers['Set-Cookie'] = 'anon_cart_key=%s; expires=31-May 2010' % self.anon_key

    def get(self):
        self.initAnonKey()
        self.body()
        
    def footer(self):
        render('Views/footer.html', self, {})
        
    def header(self, title, active):
        """ active is the page we're currently on. It must be from the page enum
            Callers must set any messages they want displayed to the user BEFORE calling this
         """
        
        # validate that active is a real page?
        menu_items = [{'href': '/', 'name': self.HOME}, {'href': '/cart', 'name': self.CART }]
        
        if self.user:
            self.add_message("Welcome, %s" % self.user.nickname(), self.MSG_NOTICE)
            menu_items.append({'name': 'Log out', 'href': users.create_logout_url('/')})
        else:
            menu_items.append({'name': 'Log in', 'href': users.create_login_url('/')})
        
        self.response.headers['Content-Type'] = 'text/html'
        self.response.headers['Expires'] = "10 Dec 2020 20:00:00 GMT" # doesn't seem to work
        
        render('Views/header.html', self, {'title': title, 'menu_items': menu_items, 'active': active})
        self.render_messages()
        render('Views/title.html', self, {'title': title})
        

